import component from "element-plus/es/components/tree-select/src/tree-select-option.mjs";
import { createRouter, createWebHashHistory } from "vue-router";
import { unauthorized} from "@/request"
const routes = [
  {
    path: "/",
    name: "loginPanel",
    component: ()=>import("@/views/LoginPanelView.vue"),
    children: [
      {
        path: "",
        name: "login-page",
        component: ()=>import("@/views/loginPanel/Login.vue")
      },{
        path: "/register",
        name: "register-page",
        component: ()=>import("@/views/loginPanel/Register.vue")
      }
    ]
  },{
    path:"/index",
    name:"index",
    component:()=> import("@/views/IndexView.vue")
  }
];

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes
});

router.beforeEach((to, from,next)=>{
    const isUnauthorized = unauthorized()
    console.log("isUnauthorized",isUnauthorized)
    if((to.name.startsWith("login")|| to.name.startsWith("register"))
        && !isUnauthorized
    ){
        next("/index")
    }else if(to.fullPath.startsWith("/index")&&isUnauthorized){
        next("/")
    }else{
        next()
    }
    
});


export default router;